package com.xcyy.zjdxmjk.zjdxmjk.controller;

import com.xcyy.zjdxmjk.zjdxmjk.VO.ResultVO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.XzqVO;
import com.xcyy.zjdxmjk.zjdxmjk.common.ResultVOUtil;
import com.xcyy.zjdxmjk.zjdxmjk.entity.City;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Country;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Town;
import com.xcyy.zjdxmjk.zjdxmjk.service.CityService;
import com.xcyy.zjdxmjk.zjdxmjk.service.CountryService;
import com.xcyy.zjdxmjk.zjdxmjk.service.TownService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CityController {
    @Autowired
    private CityService cityService;
    @Autowired
    private TownService townService;
    @Autowired
    private CountryService countryService;

    @PostMapping("/addcity")
    private ResultVO addCity(City city){
        City result=cityService.save(city);
        if (result==null)
        {
            return ResultVOUtil.error(400,"添加失败");
        }

        return ResultVOUtil.success(result);
    }

    @PostMapping("/xzq")
    private ResultVO xzq(){

        List<City>  cities=cityService.findAll();
        List<Country> countries=countryService.findAll();
        List<Town> towns=townService.findAll();

        List<XzqVO> xzqVOS=new ArrayList<>();
        XzqVO xzqVO;
        for (City city:cities){
            xzqVO=new XzqVO();
            BeanUtils.copyProperties(city,xzqVO);
            xzqVOS.add(xzqVO);
        }
        for (Country country:countries){
            xzqVO=new XzqVO();
            BeanUtils.copyProperties(country,xzqVO);
            xzqVOS.add(xzqVO);
        }
        for (Town town:towns){
            xzqVO=new XzqVO();
            BeanUtils.copyProperties(town,xzqVO);
            xzqVOS.add(xzqVO);
        }
        return ResultVOUtil.success(xzqVOS);
    }
}
